package com.ysd.lis.service.sta.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.dto.sta.StaCultureMediumDto;
import com.ysd.lis.entity.sta.StaCultureMedium;
import com.ysd.lis.entity.sta.StaItemDict;
import com.ysd.lis.mapper.sta.StaCultureMediumMapper;
import com.ysd.lis.service.sta.StaCultureMediumService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author whx
 * @since 2025-01-17
 */
@Service
public class StaCultureMediumServiceImpl extends ServiceImpl<StaCultureMediumMapper, StaCultureMedium> implements StaCultureMediumService {
    @Autowired
    StaCultureMediumMapper bacCultureMediumMapper;

    @Override
    public List<StaCultureMedium> findBacCultureMediumList(StaCultureMedium bacCultureMedium) {
        MPJLambdaWrapper<StaCultureMedium> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(StaCultureMedium.class);
        queryWrapper.selectAs("itemDict", StaItemDict::getItemNa, StaCultureMedium::getItemNa);
        if (!StringUtils.isEmpty(bacCultureMedium.getSearchValue())) {
            queryWrapper.and(wrapper -> wrapper.like(StaCultureMedium::getCode, bacCultureMedium.getSearchValue()).or().like(StaCultureMedium::getName, bacCultureMedium.getSearchValue()).or().like(StaCultureMedium::getInputCode, bacCultureMedium.getSearchValue()));
        }
        queryWrapper.leftJoin(StaItemDict.class, "itemDict", p -> p.isNotNull(StaCultureMedium::getRptGroup).isNotNull(StaCultureMedium::getItemNo).eq(StaItemDict::getItemNo, StaCultureMedium::getItemNo));
        List<StaCultureMedium> bacCultureMedia = bacCultureMediumMapper.selectList(queryWrapper);
        /*bacCultureMedia.forEach(a->{
            LabItemDict labItemDict = labItemDictMapper.selectById(a.getProjectId());
            a.setProjectName(labItemDict==null?"":labItemDict.getItemNa());
        });*/
        return bacCultureMedia;
    }

    @Override
    public Result addBacCultureMedium(StaCultureMedium bacCultureMedium) {
        return Result.succ(bacCultureMediumMapper.insert(bacCultureMedium));
    }

    @Override
    public Result deleteBacCultureMedium(StaCultureMediumDto bacCultureMediumDto) {
        List<StaCultureMedium> bacCultureMedia = bacCultureMediumDto.getStaCultureMedia();

        List<String> ids = bacCultureMedia.stream().map(StaCultureMedium::getId).collect(Collectors.toList());

        boolean b = this.removeByIds(ids);
        if (b) {
            return Result.succ(1, "删除成功", null);
        } else {
            return Result.fail(400, "删除失败", "");
        }
    }

    @Override
    public Result updateBacCultureMedium(StaCultureMedium bacCultureMedium) {
        return Result.succ(bacCultureMediumMapper.updateById(bacCultureMedium));
    }

    @Override
    public Result findBacCultureMediumListByIds(List<String> ids) {
        List<StaCultureMedium> bacCultureMediumList = bacCultureMediumMapper.selectBatchIds(ids);
        return Result.fail(1, "查询成功", bacCultureMediumList);
    }

    @Override
    public StaCultureMedium findFirstItemByNo(String no) {
        if (StringUtils.isNotBlank(no)) {
            MPJLambdaWrapper<StaCultureMedium> queryWrapper = new MPJLambdaWrapper<>();
            queryWrapper.eq(StaCultureMedium::getDelFlag, 0);
            queryWrapper.eq(StaCultureMedium::getCode, no);
            queryWrapper.orderByAsc(StaCultureMedium::getOrderNum);
            List<StaCultureMedium> lst = bacCultureMediumMapper.selectList(queryWrapper);
            if (lst.isEmpty()) {
                return null;
            }
            return lst.get(0);
        }
        return null;
    }
}
